package com.studyspringcloud.order.service.impl;

import com.studyspringcloud.order.dto.OrderDTO;
import com.studyspringcloud.order.enums.OrderStatusEnum;
import com.studyspringcloud.order.enums.PayStatusEnum;
import com.studyspringcloud.order.model.OrderMaster;
import com.studyspringcloud.order.repository.OrderDetailRepository;
import com.studyspringcloud.order.repository.OrderMasterRepository;
import com.studyspringcloud.order.service.OrderService;
import com.studyspringcloud.order.utils.KeyUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderDetailRepository orderDetailRepository;

    @Autowired
    private OrderMasterRepository orderMasterRepository;


    @Override
    public OrderDTO create(OrderDTO dto) {
        OrderMaster master = new OrderMaster();
        BeanUtils.copyProperties(dto,master);
        master.setId(KeyUtil.getUniqueKey());
        dto.setId(master.getId());
        master.setOrderAmount(new BigDecimal(1222.12));
        master.setOrderStatus(OrderStatusEnum.NEW.getCode());
        master.setPayStatus(PayStatusEnum.WAIT.getCode());
        master.setCreateTime(new Date());
        master.setUpdateTime(new Date());
        orderMasterRepository.save(master);
        return dto;
    }
}
